//
//  IndexCollentionCell.m
//  LoveCar
//
//  Created by Xue Senhong on 15/5/21.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "IndexCollentionCell.h"
#import "ArticleCell.h"
#import "NetworkHandler.h"
#import "MJRefreshHeaderView.h"
#import "MJRefreshFooterView.h"
#import "MJRefresh.h"

@implementation IndexCollentionCell

- (void)dealloc
{
    [_tableView release];
    [_dic release];
    [_imgArr release];
    [_dataArr release];
    [_newArr release];
    [_lasttime release];
    [_autoScrollView release];
    [_delegate release];
    [_art release];
    [_secTopArray release];
    
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        // 初始化容器
        self.dic = [NSMutableDictionary dictionary];
        self.imgArr = [NSMutableArray array];
        self.dataArr = [NSMutableArray array];
        self.newArr = [NSMutableArray array];
        self.secTopArray = [NSMutableArray array];
        
        // tableView
        self.tableView = [[UITableView alloc] init];
        [self.tableView setDelegate:self];
        [self.tableView setDataSource:self];
        [self.contentView addSubview:self.tableView];
        self.tableView.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)] autorelease];
        // 注册tableViewCell
        [self.tableView registerClass:[ArticleCell class] forCellReuseIdentifier:@"reuse"];
        [self.tableView setScrollsToTop:YES];
        [_tableView release];
        
        // 轮播广告
        self.autoScrollView = [[AutoScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 200)];
        [self.autoScrollView setTimeInterval:5];
        [self.tableView.tableHeaderView addSubview:self.autoScrollView];
        [_autoScrollView release];
        
        [self setupRefresh];

    }
    return self;
}

// 加载数据
- (void)reloadDataForCell
{
    // 数据源
    NSString *index = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt0-p1-s20-l0.html";
    
    [NetworkHandler connectNetGETDataWithURLString:index completion:^(id result) {
        
        self.dic = [result objectForKey:@"result"];
        self.dataArr = [self.dic objectForKey:@"newslist"];
        self.pageIndex = [[self.dic objectForKey:@"pageindex"] integerValue];
        self.lasttime = [[self.dataArr objectAtIndex:[self.dataArr count] - 1] objectForKey:@"lasttime"];
        
        [self.tableView reloadData];
    
        [self.tableView headerEndRefreshing];
    }];
}

- (void)reloadDataForAutoScrollView
{
    // 数据源
    NSString *index = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt0-p1-s20-l0.html";
    [NetworkHandler connectNetGETDataWithURLString:index completion:^(id result) {
        self.dic = [result objectForKey:@"result"];
        // 使用循环将轮播图的url放入数组
        for (int i = 0; i < [[self.dic objectForKey:@"focusimg"] count]; i++) {
            [self.imgArr addObject:[[[self.dic objectForKey:@"focusimg"] objectAtIndex:i] objectForKey:@"imgurl"]];
            
            [self.secTopArray addObject:[[[self.dic objectForKey:@"focusimg"] objectAtIndex:i] objectForKey:@"id"]];
            
            NSLog(@"%@", self.secTopArray);
        }
        
        if (self.secTopArray.count == 0) {
            NSLog(@"nil");
        } else {
        // 给轮播图指定图片url
            [self.autoScrollView setImageUrls:self.imgArr];
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAciton:)];
            [self.autoScrollView addGestureRecognizer:tap];
            [tap release];
        }
    }];
    
}

- (void)tapAciton:(id)sender
{
    if (self.autoScrollView.pageNo == 0) {
        [self.delegate pushViewControllerWithString:[self.secTopArray objectAtIndex:0]];
    } if (self.autoScrollView.pageNo == 1) {
        [self.delegate pushViewControllerWithString:[self.secTopArray objectAtIndex:1]];
    } if (self.autoScrollView.pageNo == 2) {
        [self.delegate pushViewControllerWithString:[self.secTopArray objectAtIndex:2]];
    } if (self.autoScrollView.pageNo == 3) {
        [self.delegate pushViewControllerWithString:[self.secTopArray objectAtIndex:3]];
    }
}


// 页面布局
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.tableView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 113)];
}

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    
//#warning 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 加载轮播图
    [self reloadDataForAutoScrollView];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
//    [self.dataArr removeAllObjects];
    [self reloadDataForCell];
    
    // 刷新表格
//    [self.tableView reloadData];
    
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
//    [self.tableView headerEndRefreshing];


}

- (void)footerRereshing
{
    // 数据源
    NSString *index = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt0-p%ld-s20-l%@.html", self.pageIndex + 1, self.lasttime];
    [NetworkHandler connectNetGETDataWithURLString:index completion:^(id result) {
        
        self.dic = [result objectForKey:@"result"];
        self.newArr = [self.dic objectForKey:@"newslist"];
        self.pageIndex = [[self.dic objectForKey:@"pageindex"] integerValue];
        self.lasttime = [[self.newArr objectAtIndex:[self.newArr count] - 1] objectForKey:@"lasttime"];
        
        [self.dataArr addObjectsFromArray:self.newArr];
        [self.tableView reloadData];
        
    }];
    
    // 刷新表格
    [self.tableView reloadData];
    
    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [self.tableView footerEndRefreshing];

}

#pragma mark - tableView 协议方法
// tableViewCell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}
// tableViewCell方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    NSMutableDictionary *dic = [self.dataArr objectAtIndex:indexPath.row];
    // KVC赋值
    self.art = [[Article alloc] init];
    [self.art setValuesForKeysWithDictionary:dic];
    [cell valueforCellWithArticle:self.art];
    [_art release];
    return cell;
}
// tableViewCell数量(每个section中)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataArr count];
}
// cell选择方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 取消选中cell效果
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 让代理人调用方法
    [self.delegate pushViewControllerWithString:[[self.dataArr objectAtIndex:indexPath.row] objectForKey:@"id"]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
